// 报销统计
import { pool } from "@/db";
import reswrapper from "../reswrapper";
import { v1 as uuidv1 } from "uuid";
import { RES_MSG } from "@/interfaces/resmsg";
import { formatDateTime } from "@/utils";

export async function updateReceiptStatus(ctx, next) {
  const { id, status, paydate } = ctx.request.body;
  const sql = `UPDATE travel SET status = ${status} ${
    paydate ? `, paydate='${paydate}'` : ""
  }  WHERE id='${id}'`;
  const res = await pool.query(sql);
  if (res.rowCount > 0) {
    ctx.body = reswrapper(id);
  } else {
    ctx.body = reswrapper(null, RES_MSG.FAIL, "操作失败");
  }
}

/**
 * 删除单条
 * @param ctx
 * @param next
 */
export async function updateReceipt(ctx, next) {
  const {
    id,
    region,
    regionType,
    startDate,
    endDate,
    days,
    zsf,
    jtf,
    zzc,
    payment,
    remark,
  } = ctx.request.body;
  const sql = `UPDATE travel SET 
    region = '${region}', 
    "regionType" = ${regionType},
    "startDate" = '${startDate}', 
    "endDate" = '${endDate}',
    days = ${days}, 
    jtf = ${jtf},
    zsf = ${zsf},
    zzc = ${zzc}, 
    payment = ${payment},
    remark  = '${remark}'
  WHERE id='${id}'`;
  console.log(sql);
  const res = await pool.query(sql);
  if (res.rowCount > 0) {
    ctx.body = reswrapper(id);
  } else {
    ctx.body = reswrapper(null, RES_MSG.FAIL, "操作失败");
  }
}

/**
 * 删除单条
 * @param ctx
 * @param next
 */
export async function deleteReceipt(ctx, next) {
  const { id } = ctx.query;
  // console.log(id, ctx.params, ctx.query);
  const sql = `DELETE FROM travel WHERE id = '${id}'`;
  const res = await pool.query(sql);
  if (res.rowCount > 0) {
    ctx.body = reswrapper(id);
  } else {
    ctx.body = reswrapper(null, RES_MSG.FAIL, "操作失败");
  }
}

/**
 * 获取报销列表
 * @param ctx
 * @param next
 */
export async function receiptList(ctx, next) {
  const { pageNum, pageSize } = ctx.query;
  const start = (Number(pageNum) - 1) * Number(pageSize);
  const sql = `SELECT * FROM travel where 1=1 ORDER BY "createTime" DESC OFFSET ${start} LIMIT ${pageSize} `; //分页  select * from travel where 1=1
  const sql2 = `select COUNT(id) from travel`;
  const res2 = await pool.query(sql2);
  const total = parseInt(res2.rows[0].count);
  const res = await pool.query(sql);
  const list = res.rows.map((v) => {
    return {
      ...v,
      startDate: formatDateTime(v.startDate, "yyyy-MM-dd"),
      endDate: formatDateTime(v.endDate, "yyyy-MM-dd"),
      createTime: formatDateTime(v.createTime, "yyyy-MM-dd HH:mm:ss"),
      zsf: Number(v.zsf.replace(/\$|\,/g, "")),
      jtf: Number(v.jtf.replace(/\$|\,/g, "")),
      zzc: Number(v.zzc.replace(/\$|\,/g, "")),
      payment: Number(v.payment.replace(/\$|\,/g, "")),
      paydate: v.paydate ? formatDateTime(v.paydate, "yyyy-MM-dd") : "",
    };
  });
  ctx.body = reswrapper({
    list,
    total,
    pageNum: Number(pageNum),
    pageSize: Number(pageSize),
  });
}

/**
 * 录入报销信息
 * @param ctx
 * @param next
 */
export async function insertReceipt(ctx, next) {
  const {
    region,
    regionType,
    startDate,
    endDate,
    days,
    zsf,
    jtf,
    zzc,
    payment,
  } = ctx.request.body;
  const id = uuidv1(); //uuidv4(); //生成唯一ID
  const remark = ctx.request.body.remark || "";
  const sql = `INSERT INTO travel (id, status, region, "regionType", "startDate", "endDate", days, zsf, jtf, zzc, payment, remark, "createTime")  

VALUES ('${id}', 1, '${region}', ${regionType}, '${startDate}', '${endDate}', ${days}, ${zsf}, ${jtf}, ${zzc}, ${payment}, '${remark}', '${formatDateTime(
    new Date(),
    "yyyy-MM-dd HH:mm:ss"
  )}')`;
  const res = await pool.query(sql);
  if (res.rowCount > 0) {
    ctx.body = reswrapper(id);
  } else {
    ctx.body = reswrapper(null, RES_MSG.FAIL, "操作失败");
  }
}

/**
 * 获取状态字典列表
 *
 * @param ctx - Koa 上下文对象
 * @param next - Koa 中间件的下一个函数
 * @returns 包含查询结果的响应对象
 */
export async function getStatus(ctx, next) {
  const sql = `select id, title, value, remark from dict where category = 'REIM_FORM_STATUS'`;
  const res = await pool.query(sql);
  ctx.body = reswrapper(res.rows);
}

/**
 * 查询地区类型
 * @param ctx
 * @param next
 */
export async function regionType(ctx, next) {
  const sql = `select id, title, value, remark from dict where category = 'REGION_TYPE'`;
  const res = await pool.query(sql);
  ctx.body = reswrapper(res.rows);
}
